<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use DB;

class SendMessage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $template;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($templateId)
    {
        $this->template = $templateId;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $tempinfo= $this->TempInfo($this->template);
        $user = DB::table('user')->where('status','1')->get();
        foreach($user as $v){
            $template = DB::table('template_message')
                ->where('open_id','=',$v->open_id)
                ->where('form_id','!=','0')
                ->orderBy('id', 'desc')->first();
            if($template){
                $openid = $template->open_id;
                $formid = $template->form_id;
            }

            $data = array(
                'touser' => $openid, // openid是发送消息的基础
                'template_id' => 'VWdIGrhdwTspH-wBgDm7auxulZXGwPbofuk31ihdmAw', // 模板id
                'form_id' => $formid,
                'page' => 'pages/index/index', // 点击跳转地址
                'topcolor' => '#FF0000', // 顶部颜色
                'data' => array(
                    'keyword1' => array('value' => $tempinfo['1']),
                    'keyword2' => array('value' => $tempinfo['2']),
                    'keyword3' => array('value' => $tempinfo['3']),
                    'keyword4' => array('value' => $tempinfo['4']),
                    'keyword5' => array('value' => $tempinfo['5']),
                    'keyword6' => array('value' => $tempinfo['6']),
                    'keyword7' => array('value' => $tempinfo['7']),
                )
            );
             $a = $this->AccessToken($data);
             dd($a);
        }
    }

    public function TempInfo($tempmessageId){
        $tempmessage = DB::table('template_message_records')->where('id',$tempmessageId)->first();
        $type = $tempmessage->type;
        $parameter_one = $tempmessage->parameter_one;
        $parameter_two = $tempmessage->parameter_two;
        $parameter_three = $tempmessage->parameter_three;
        $parameter_four = $tempmessage->parameter_four;
        $parameter_five = $tempmessage->parameter_five;
        $parameter_six = $tempmessage->parameter_six;
        $parameter_seven = $tempmessage->parameter_seven;
        $data = [];
        $data[] = array_push($data,$type,$parameter_one,$parameter_two,$parameter_three,$parameter_four,$parameter_five,$parameter_six,$parameter_seven);
        return $data;
    }

    public function AccessToken($data){
        $appid = "wxd86b79990ee410d4";
        $secret = "7c7e4a98d8832c55b5a53b86b6830a2c";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $res = json_decode(file_get_contents($url));
        $access_token = $res->access_token;

        $urls = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$access_token";
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $urls);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode(json_encode($data)));
        $rest = curl_exec($ch);//运行curl
        curl_close($ch);
        return $rest;
    }
}
